﻿package com.tvein.sanguo.swc.action.tool
{
	//flash
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	import fl.controls.Slider;
	import fl.events.SliderEvent;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class ToolGold extends MovieClip
	{
		public static const NAME:String = "Tool-Gold";

		private var effectVol:int = 100;
		private var overLay:OverLay;
		
		private var changeSlider:Slider;
		private var point:int;
		
		public function ToolGold()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			txtPoint.border = true;
			txtPoint.borderColor = 0x333333;
			txtGold.border = true;
			txtGold.borderColor = 0x333333;
			
			var style:TextFormat = new TextFormat();
			style.color = 0xffffff;
			style.size = 18;
			style.font = "LiSu";
			style.align = "center";
			
			txtPoint.setTextFormat(style);
			txtGold.setTextFormat(style);
			
			changeSlider = new Slider();
			changeSlider.name = "changeSlider";
			changeSlider.x = 130;
			changeSlider.y = 240;
			changeSlider.width = 340;
			changeSlider.height = 3;
			changeSlider.maximum = 100;
			changeSlider.minimum = 0;
			changeSlider.snapInterval = 1;
			changeSlider.tickInterval = 10;
			this.addChild(changeSlider);
			
			btnSend.visible = false;
			btnMinus.visible = false;
			btnPlus.visible = false;
			
			changeSlider.addEventListener(SliderEvent.CHANGE, onChange);
			btnMinus.addEventListener(MouseEvent.CLICK, onClick);
			btnPlus.addEventListener(MouseEvent.CLICK, onClick);
			btnBack.addEventListener(MouseEvent.CLICK, onClick);
			btnSend.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setData":
					point = params.Process[0].Value;
					txtPoint.text = params.Process[0].Value;
					
					reRoad();
					break;
			}
		}
		
		private function reRoad():void
		{
			if (changeSlider.value == 0)
			{
				btnMinus.visible = false;
				btnSend.visible = false;
			}
			else
			{
				btnMinus.visible = true;
				btnSend.visible = true;
			}

			if (changeSlider.value < point)
			{
				btnPlus.visible = true;
			}
			else
			{
				btnPlus.visible = false;
			}
			
			txtGold.text = String(changeSlider.value);
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			changeSlider.removeEventListener(SliderEvent.CHANGE, onChange);
			btnMinus.removeEventListener(MouseEvent.CLICK, onClick);
			btnPlus.removeEventListener(MouseEvent.CLICK, onClick);
			btnBack.removeEventListener(MouseEvent.CLICK, onClick);
			btnSend.removeEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function _resizeUI(evt:Event = null):void
		{			
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}

		private function onChange(evt:SliderEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			reRoad();
		}
		
		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnBack":
					dispatchEvent(new SwcEvent(NAME, ["-close", "ToolGold"]));
					break;
				case "btnSend":
					dispatchEvent(new SwcEvent(NAME, ["-gameCommand", "ExchangePoint(" + changeSlider.value + ");"], true));
					break;
				case "btnMinus":
					changeSlider.value --;
					reRoad();
					break;
				case "btnPlus":
					changeSlider.value ++;
					reRoad();
					break;
			}
		}
	}
}